﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "QtNotificationBar.h"

#include "GbmpNew.h"
#include "TransferOwnership.h"
#include "UiNotificationManager.h"

#include <QBoxLayout>
#include <QPushButton>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;


namespace
{
    const double s_opacity = 0.65;
    const int s_iconSize = 16;
    const int s_spacing = 6;
}

// QtNotificationBar 类实现
gcmp::QtNotificationBar::QtNotificationBar(QWidget* pParent) :
    QWidget(pParent, Qt::FramelessWindowHint | Qt::Tool)
{
    setContentsMargins(s_spacing, s_spacing, s_spacing, s_spacing);
    setWindowOpacity(s_opacity);
    setStyleSheet("background-color: rgba(0, 0, 0, 120);");

    OwnerPtr<QPushButton> oClose = NEW_AS_OWNER_PTR(QPushButton);
    oClose->setFixedSize(s_iconSize, s_iconSize);
    oClose->setIcon(QIcon(":/images/notification/close.png"));
    oClose->setCursor(QCursor(Qt::PointingHandCursor));
    QObject::connect(oClose.get(), SIGNAL(clicked()), this, SLOT(OnCloseNotification()));

    OwnerPtr<QPushButton> oHistory = NEW_AS_OWNER_PTR(QPushButton);
    oHistory->setFixedSize(s_iconSize, s_iconSize);
    oHistory->setIcon(QIcon(":/images/notification/history.png"));
    oHistory->setCursor(QCursor(Qt::PointingHandCursor));
    QObject::connect(oHistory.get(), SIGNAL(clicked()), this, SLOT(OnShowHistory()));

    OwnerPtr<QVBoxLayout> oLayout = NEW_AS_OWNER_PTR(QVBoxLayout);
    oLayout->setContentsMargins(0, 0, 0, 0);
    oLayout->setSpacing(s_spacing);
    oLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(oHistory));
    oLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(oClose));

    setLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(oLayout));
}

void gcmp::QtNotificationBar::keyPressEvent(QKeyEvent * event)
{
    this->parent()->event((QEvent*)event);
}

void gcmp::QtNotificationBar::OnCloseNotification()
{
    UiNotificationManager::Get()->CloseNotification();
}

void gcmp::QtNotificationBar::OnShowHistory()
{
    UiNotificationManager::Get()->ShowHistory();
}
